package coderead.tomcat;
/**
 * @Copyright 源码阅读网 http://coderead.cn
 */

import org.apache.catalina.*;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.core.StandardEngine;
import org.apache.catalina.core.StandardHost;
import org.apache.catalina.core.StandardService;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.webresources.DirResourceSet;
import org.apache.catalina.webresources.FileResourceSet;
import org.apache.catalina.webresources.StandardRoot;
import org.junit.Test;

import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import java.io.File;

/**
 * @author 鲁班大叔
 * @date 2021
 */
public class Application {

    @Test
    public void normalTest() throws LifecycleException {
        Tomcat tomcat = new Tomcat();
        tomcat.addWebapp("", "/Users/tommy/git/coderead-tomcat/src/main/java/coderead/tomcat");
        tomcat.getConnector();
        // 添加连接器
        /*Connector connector = new Connector("HTTP/1.1");
        connector.setPort(8080);
        tomcat.getService().addConnector(connector);*/

        tomcat.start();
        tomcat.getServer().await();
    }

    // 标准写法
    @Test
    public void normalTest2() throws LifecycleException {
        Tomcat tomcat = new Tomcat();
        // 添加服务
       /* Server server = tomcat.getServer();
        StandardService service = new StandardService();
        server.addService(service);

        Engine engine=new StandardEngine();
        engine.setDefaultHost("www.luban.com");
        engine.setService(service);
        engine.setName("Catalina");

        service.setContainer(engine);

        StandardHost host = new StandardHost();
        host.setAppBase("/Users/tommy/data/www");
        host.setName("www.luban.com");
        host.setAutoDeploy(true);
        host.setUnpackWARs(true);
        engine.addChild(host);*/
        /*StandardHost host = new StandardHost();
        host.setAppBase("/Users/tommy/data/www/");
        host.setName("www.luban.com");
        host.setAutoDeploy(true);
        host.setUnpackWARs(true);
        tomcat.getEngine().addChild(host);*/

        tomcat.getHost().setAppBase("/Users/tommy/data/www/");
        tomcat.getHost().setAutoDeploy(true);

        Connector connector = new Connector("HTTP/1.1");
        connector.setPort(8080);
        tomcat.getService().addConnector(connector);

        tomcat.start();
        tomcat.getServer().await();
    }

    public static void main(String[] args) throws LifecycleException, ServletException {
        test();
    }

    public static void test() throws LifecycleException, ServletException {
        Tomcat tomcat = new Tomcat();
        Context context = tomcat.addWebapp("",
                "/Users/tommy/git/coderead-tomcat");
        WebResourceRoot resourceRoot = new StandardRoot(context);
        String sourcePath = Application.class.getResource("/").getPath();
        resourceRoot.addPreResources(new DirResourceSet(resourceRoot,
                "/", sourcePath, "/"));
        context.setResources(resourceRoot);
        tomcat.getConnector();// 添加连接器
        tomcat.start();
        tomcat.getServer().await();
    }
}
